<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>
    <div id="app">
        <h1>{{msg}}</h1>
        <button @click='goclick'>点击按钮获取数据</button>
        <hr>
        <input type="text" v-model='inputVal'>
        <button @click='addData'>点击添加数据</button>

        <ul>
            <li v-for='item in list' v-bind:key='item.id' v-if='list.length>0'>
                {{item.name}}
            </li>
        </ul>
    </div>

    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                msg: 'hello vue',
                inputVal: '',
                list: [],
                id: 5
            },
            methods: {
                addData() {
                    if (!this.inputVal.trim()) {
                        alert('不能为空');
                        return;
                    }

                    var obj = {
                        "id": ++this.id,
                        "name": this.inputVal,
                        "author": "罗贯中",
                        "category": "文学",
                        "desc": "一个杀伐纷争的年代"
                    };
                    this.list.unshift(obj);
                    this.inputVal = '';
                },

                goclick() {
                    this.getdata();
                },

                getdata() {
                    axios.get('./data/data.json').then(res => {
                        console.log(res.data);
                        this.list = [...this.list, ...res.data];
                    })
                }


                //第一种方法先定义好函数 调用 






                //第二种方法生命周期钩子函数里面调用

            }
        })
    </script>
</body>

</html>